home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2734 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  69 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Q] Porting to ANSI C
  5. Date: Tue, 23 Jan 96 13:22:31 GMT
  6. Organization: none
  7. Message-ID: <822403351snz@genesis.demon.co.uk>
  8. References: <DLLv31.Au2@news.zippo.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <DLLv31.Au2@news.zippo.com> john "John Muller" writes:
  15.  
  16. >
  17. >Hi,
  18. >
  19. >We are attempting to port existing code to ANSI C.  However, we have
  20. >many instances of something like this:
  21. >
  22. >typedef struct {
  23. >        :
  24. >        int     len;
  25. >        char    data[0];
  26. >}
  27.  
  28. This looks like an example of what is known as the 'struct hack'. This
  29. idea is to allocate (using malloc etc.) a variable sized structure depending
  30. on the size of the data array. Strictly you can't do this in ANSI C because
  31. every object type must have a length that is known at compile time (there
  32. has been much debate on this in the past on comp.std.c). However even though
  33. it is not strictly ANSI C you can still do something like this very
  34. portably (section 2.6 of the FAQ covers this question). Possibly the best
  35. approach is something like the following:
  36.  
  37. #include <stddef.h>
  38.  
  39. #define HACK_MAX_DATA_SIZE   1000     /* Or whatever value is appropriate */
  40. #define HACK_ALLOC_SIZE(datasize)   (offsetof(struct hack, data) + (datasize))
  41.  
  42. struct hack {
  43.     ...
  44.     int     len;
  45.     char    data[HACK_MAX_DATA_SIZE];
  46. };
  47.  
  48.  
  49. Then in the program you can use something like:
  50.  
  51.    struct hack *hptr = malloc(HACK_ALLOC_SIZE(numchars));
  52.  
  53. The reason for making the declared size of .data HACK_MAX_DATA_SIZE instead
  54. of, say, 1 is to tell the compiler what range of offsets it has to support
  55. for pointer arithmetic. It would be possible (and legal) for a smart
  56. compiler to notice that an object is 'small' and use some sort of efficient
  57. 'small' pointer arithmetic calculation.
  58.  
  59. Again, this has been deemed not strictly conforming by the language committee
  60. but even so you should have no trouble using it in practice, so long
  61. as you don't ever do anything that relies on sizeof(struct hack) such as
  62. structure assignment or making it part of another struct/union/array.
  63.  
  64. -- 
  65. -----------------------------------------
  66. Lawrence Kirby | fred@genesis.demon.co.uk
  67. Wilts, England | 70734.126@compuserve.com
  68. -----------------------------------------
  69.